package com.cmsky.open.cms.service.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.cmsky.open.base.model.ValidEnum;
import com.cmsky.open.cms.enums.CmsNodePropertyKey;
import com.cmsky.open.cms.enums.CmsNodeType;
import com.cmsky.open.cms.manager.CmsNodeInfoManager;
import com.cmsky.open.cms.manager.CmsNodePropertyInfoManager;
import com.cmsky.open.cms.model.CmsNodeInfo;
import com.cmsky.open.cms.model.CmsNodePropertyInfo;
import com.cmsky.open.cms.model.GoodsNode;
import com.cmsky.open.cms.model.PicHrefNode;
import com.cmsky.open.cms.model.SimpleNode;
import com.cmsky.open.cms.service.CmsNodeService;
import com.cmsky.open.cms.vo.query.CmsNodeInfoQuery;
import com.cmsky.open.cms.vo.query.CmsNodePropertyInfoQuery;

@Service("cmsNodeService")
public class CmsNodeServiceImpl implements CmsNodeService{
	static Logger logger = Logger.getLogger(CmsNodeService.class);
	private CmsNodeInfoManager cmsNodeInfoManager;
	private CmsNodePropertyInfoManager cmsNodePropertyInfoManager;
	public void setCmsNodeInfoManager(CmsNodeInfoManager cmsNodeInfoManager) {
		this.cmsNodeInfoManager = cmsNodeInfoManager;
	}
	public void setCmsNodePropertyInfoManager(
			CmsNodePropertyInfoManager cmsNodePropertyInfoManager) {
		this.cmsNodePropertyInfoManager = cmsNodePropertyInfoManager;
	}
	
	public List<CmsNodeInfo> findByPid(Integer pid) {
		List<CmsNodeInfo> result = null;
		if(null!=pid){
			CmsNodeInfoQuery query = new CmsNodeInfoQuery();
			query.setPid(pid);
			query.setSortColumns("sid");
			query.setValid(ValidEnum.Valid.getValue());
			List<CmsNodeInfo> list = (List<CmsNodeInfo>) this.cmsNodeInfoManager.findAllByQuery(query);
			if(null!=list && 0 < list.size()){
				result = new LinkedList<CmsNodeInfo>();
				CmsNodeInfo temp = null;
				for(CmsNodeInfo c : list){
					temp = parseToItemType(c);
					result.add(temp);
				}
			}
		}
		return result;
	}

	public CmsNodeInfo findById(Integer id) {
		CmsNodeInfo nodeInfo = null;
		if(null!=id){
			nodeInfo = cmsNodeInfoManager.getById(id);
			if(null!=nodeInfo && nodeInfo.getProperties()==null){
				nodeInfo.setProperties(this.findProperties(id));
			}
		}
		return parseToItemType(nodeInfo);
	}

	public CmsNodeInfo findByPidandSid(Integer pid, Integer sid) {
		CmsNodeInfo cmsNodeInfo = null;
		if(null!=pid && null!=sid){
			CmsNodeInfoQuery query = new CmsNodeInfoQuery();
			query.setPid(pid);
			query.setSortColumns("sid");
			List<CmsNodeInfo> list = (List<CmsNodeInfo>) this.cmsNodeInfoManager.findAllByQuery(query);
			if(0!=list.size()){
				cmsNodeInfo = list.get(0);
				cmsNodeInfo.setProperties(this.findProperties(cmsNodeInfo.getId()));
			}
		}
		return parseToItemType(cmsNodeInfo);
	}
	public Map<CmsNodePropertyKey, String> findProperties(Integer id) {
		Map<CmsNodePropertyKey, String> result = null;
		if(id!=null){
			result = new HashMap<CmsNodePropertyKey, String>();
			CmsNodePropertyInfoQuery query = new CmsNodePropertyInfoQuery();
			query.setNid(id);
			List<CmsNodePropertyInfo> properties= (List<CmsNodePropertyInfo>) this.cmsNodePropertyInfoManager.findAllByQuery(query);
			for(CmsNodePropertyInfo p : properties){
				CmsNodePropertyKey key = CmsNodePropertyKey.getByKey(p.getName());
				if(null!=key){
					result.put(key, p.getVal());
				}
			}
		}
		return result;
	}
	public Map<Integer, CmsNodeInfo> findByPidForSid(Integer pid) {
		Map<Integer, CmsNodeInfo> result = null;
		List<CmsNodeInfo> list = this.findByPid(pid);
		if(null!=list){
			result = new HashMap<Integer,CmsNodeInfo>();
			for(CmsNodeInfo node : list){
				result.put(node.getSid(), parseToItemType(node));
			}
		}
		return result;
	}
	
	protected CmsNodeInfo parseToItemType(CmsNodeInfo node){
		CmsNodeInfo newOne = null;
		CmsNodeType nodeType = CmsNodeType.getByValue(node.getType());
		if(null == node.getProperties()){
			node.setProperties(this.findProperties(node.getId()));
		}
		if(null != node.getProperties()){
			if(CmsNodeType.GoodsNode == nodeType){
				GoodsNode goods = new GoodsNode();
				if(node.getProperties().containsKey(CmsNodePropertyKey.GoodsId)){
					try {
						goods.setGoodsId(Integer.parseInt(node.getProperties()
								.get(CmsNodePropertyKey.GoodsId)));
						newOne = goods;
					} catch (Exception e) {
						logger.error(node,e);
					}
				}
			}else{
				SimpleNode simple = new SimpleNode();
				if(node.getProperties().containsKey(CmsNodePropertyKey.Title)){
					simple.setTitle(node.getProperties().get(CmsNodePropertyKey.Title));
				}else{
					simple.setTitle(node.getName());
				}
				if(node.getProperties().containsKey(CmsNodePropertyKey.Src)){
					simple.setSource(node.getProperties().get(CmsNodePropertyKey.Src));
				}
				if(node.getProperties().containsKey(CmsNodePropertyKey.Href)){
					simple.setHref(node.getProperties().get(CmsNodePropertyKey.Href));
				}
				if(node.getProperties().containsKey(CmsNodePropertyKey.Context)){
					simple.setContext(node.getProperties().get(CmsNodePropertyKey.Context));
				}
				newOne = simple;
			}
		}
		
		if(null==newOne){
			newOne = node;
		}else{
			newOne.setId(node.getId());
			newOne.setName(node.getName());
			newOne.setParent(node.getParent());
			newOne.setPid(node.getPid());
			newOne.setProperties(node.getProperties());
			newOne.setSid(node.getSid());
			newOne.setType(node.getType());
			newOne.setValid(node.getValid());
		}
		
		return newOne;
	}
}
